package com.chookss.view;

import android.content.Context;
import android.net.Uri;

import com.chookss.Urls;
import com.chookss.tools.PathUtil;
import com.johnrambo.ktea.common.klogutil.KLog;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.engine.CompressFileEngine;
import com.luck.picture.lib.interfaces.OnKeyValueResultCallbackListener;
import com.luck.picture.lib.utils.DateUtils;
import com.zxy.tiny.Tiny;
import com.zxy.tiny.callback.FileCallback;

import java.io.File;
import java.util.ArrayList;

import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnNewCompressListener;
import top.zibin.luban.OnRenameListener;

public  class ImageFileCompressEngine implements CompressFileEngine {

    @Override
    public void onStartCompress(Context context, ArrayList<Uri> source, OnKeyValueResultCallbackListener call) {

//        Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
//        options.quality = 100;
//        options.compressDirectory=context.getExternalFilesDir(Urls.UPLOADIMAGE).getAbsolutePath();
//        options.size = 1024 * 1024;
//        for (int i = 0; i < source.size(); i++) {
//            int j=i;
//            Tiny.getInstance().source(source.get(j)).asFile().withOptions(options).compress(new FileCallback() {
//                @Override
//                public void callback(boolean isSuccess, String outfile, Throwable t) {
//                    if (isSuccess) {
//                        if (call != null) {
//                            call.onCallback(source.get(j).toString(),outfile);
//                        }
//                    } else {
//                        if (call != null) {
//                            call.onCallback(source.get(j).toString(),"");
//                        }
//                    }
//                }
//            });
//        }
        Luban.with(context).load(source).ignoreBy(1024).setTargetDir(context.getExternalFilesDir(Urls.UPLOADIMAGE).getAbsolutePath()).setRenameListener(new OnRenameListener() {
            @Override
            public String rename(String filePath) {
                int indexOf = filePath.lastIndexOf(".");
                String postfix = indexOf != -1 ? filePath.substring(indexOf) : ".jpg";
                return DateUtils.getCreateFileName("CMP_") + postfix;
            }
        }).filter(new CompressionPredicate() {
            @Override
            public boolean apply(String path) {
                if (PictureMimeType.isUrlHasImage(path) && !PictureMimeType.isHasHttp(path)) {
                    return true;
                }
                return !PictureMimeType.isUrlHasGif(path);
            }
        }).setCompressListener(new OnNewCompressListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(String source, File compressFile) {
                if (call != null) {
                    KLog.i(source);
                    KLog.i(compressFile.getAbsolutePath());
                    call.onCallback(source, compressFile.getAbsolutePath());
                }
            }

            @Override
            public void onError(String source, Throwable e) {
                if (call != null) {
                    call.onCallback(source, null);
                }
            }
        }).launch();
    }
    private ImageFileCompressEngine() {
    }

    private static final class InstanceHolder {
        static final ImageFileCompressEngine instance = new ImageFileCompressEngine();
    }

    public static ImageFileCompressEngine createGlideEngine() {
        return ImageFileCompressEngine.InstanceHolder.instance;
    }
}